package com.yvon.maple.service.doc.controller;

import cn.hutool.core.collection.CollUtil;
import com.yvon.maple.service.doc.dto.ArticleTitleDTO;
import com.yvon.maple.service.doc.dto.SidebarDTO;
import com.yvon.maple.service.doc.entity.Article;
import com.yvon.maple.service.doc.entity.Category;
import com.yvon.maple.service.doc.service.ArticleService;
import com.yvon.maple.service.doc.service.CategoryService;
import io.swagger.annotations.Api;
import lombok.RequiredArgsConstructor;
import lombok.SneakyThrows;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;

import java.util.ArrayList;
import java.util.Comparator;
import java.util.List;
import java.util.stream.Collectors;

/**
 * @author : Yvon / Yvon@seerbigdata.com
 * @version : 1.0
 * @since : 2020/9/8
 */
@Controller
@RequiredArgsConstructor
@Api(tags = "Ftl调用接口", hidden = true)
public class ArticleFtlController {

    @Autowired
    private ArticleService articleService;

    @Autowired
    private CategoryService categoryService;

    /**
     * 侧边栏
     *
     * @param model 模型
     * @return : String
     * @author : Yvon / 2020-09-08
     */
    @SneakyThrows
    @GetMapping("/sidebar.md")
    public String sidebar(Model model) {

        List<SidebarDTO> sidebarDTOList = new ArrayList<>();

        List<Category> categoryList =  categoryService.getAll();

        categoryList.stream().sorted(Comparator.comparing(Category::getSort)).forEach(category -> {
            SidebarDTO sidebarDTO = new SidebarDTO();
            sidebarDTO.setCategoryName(category.getName());
            List<Article> sidebar = articleService.getSidebar(category.getId());
            if (CollUtil.isNotEmpty(sidebar)) {
                List<ArticleTitleDTO> articleTitleList = new ArrayList<>();

                sidebar.stream().sorted(Comparator.comparing(Article::getSort)).forEach(article -> {
                    ArticleTitleDTO articleTitleDTO = new ArticleTitleDTO();
                    articleTitleDTO.setArticleId(article.getId());
                    articleTitleDTO.setArticleName(article.getName());
                    articleTitleList.add(articleTitleDTO);

                });
                sidebarDTO.setArticleTitle(articleTitleList);
                sidebarDTOList.add(sidebarDTO);
            }
        });


        model.addAttribute("list", sidebarDTOList);
        return "sidebar";
    }

    @GetMapping("/article/{articleId}.md")
    public String articleContent(Model model, @PathVariable String articleId) {

        Article article = articleService.getById(articleId);
        model.addAttribute("article", article);
        return "tableInfo";
    }


    @RequestMapping("/index")
    public String index(Model model) {
        return "/index";
    }


    @GetMapping("/coverpage.md")
    public String coverpage(Model model) {
        return "coverpage";
    }

}